package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.ACB;

public class ACBPresenter extends AbstractPresenter<ACB> {
	final String SINGLEITEM = "acb";
	final String MULTIITEM = "acbs";
	
	public ArrayList<ACB> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public ACB tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public ACB createItemFromJSONObject(JSONObject jsonObject){
		ACB item = new ACB();
		try{
			item.setAcbID(jsonObject.getInt("ACBID"));
			item.setAcbQty(jsonObject.getInt("ACBQty"));
			item.setAcbFilled(jsonObject.getBoolean("ACBFilled"));
			item.setVesselVolume(jsonObject.getDouble("servingTankVolume"));
			
			FGIViewPresenter fgiPresenter = new FGIViewPresenter();
			JSONObject jsonFGI = jsonObject.getJSONObject("ACBFGI");
			item.setFgi(fgiPresenter.createItemFromJSONObject(jsonFGI));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(ACB item){
		try{
			JSONObject json = new JSONObject();
			json.put("ACBID", item.getAcbID());
			json.put("ACBQty", item.getAcbQty());
			json.put("ACBFilled", item.isAcbFilled());
			json.put("servingTankVolume", item.getVesselVolume());
			
			FGIViewPresenter fgiPresenter = new FGIViewPresenter();
			json.put("ACBFGI", fgiPresenter.createJSON(item.getFgi()));
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public ACB getEmptyEntity(){
		ACB entity = new ACB();
		return entity;
	}
}
